package com.dreamthen.controller;

import com.dreamthen.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author zengms
 * @Date 2023-11-02 19:57:44
 **/
@Controller
@RequestMapping("/param")
public class ParamController {

    /*
    1、RequestParam接收参数名 可以跟定义的参数类型不同，需要通过value属性来设置
    2、默认情况下，required属性为true，表示参数必填，如果非必填，则将required =false
     */
    @RequestMapping(value = "/param01")
    public String param01(@RequestParam(value = "userName", required = true) String name) {

        System.out.println("hello word:" + name);
        return "redirect:/index.jsp";
    }

    /*
    如果非必填，则将required =false，当参数为空时，可以设置默认，defaultValue = "test"
     */
    @RequestMapping(value = "/param02")
    public String param02(@RequestParam(value = "userName", required = false, defaultValue = "test") String name) {

        System.out.println("hello word:" + name);
        return "redirect:/index.jsp";
    }

    /*
    通过对象来接受参数
     */
    @RequestMapping(value = "/param03")
    public String param03(User user) {
        System.out.println("hello word:" + user.toString());
        return "redirect:/index.jsp";
    }


    /**
     * 获取header域里面相关参数
     * @param agent
     * @return
     */
    @RequestMapping("/header")
    public String header( @RequestHeader(value = "User-Agent", required = false) String agent) {
        System.out.println("agent===" + agent);
        return "redirect:/index.jsp";
    }

    /**
     * 获取cookie域里面相关参数
     * @param jsessionId
     * @return
     */
    @RequestMapping("/cookie")
    public String cookie(@CookieValue(value = "JSESSIONID", required = false) String jsessionId) {
        System.out.println("jsessionId===" + jsessionId);
        return "redirect:/index.jsp";
    }

    @RequestMapping("/path1/{id}/{userName}")
    public String path1(@PathVariable("id") String ids,@PathVariable("userName") String name) {
        System.out.println("id===" + ids);
        System.out.println("userName===" + name);
        return "redirect:/index.jsp";
    }

    @RequestMapping("/path2/{id}/{name}")
    public String path2(User user) {
        System.out.println("user===" + user.toString());
        return "redirect:/index.jsp";
    }
}
